<!-- 柱上开关（柱上断路器、柱上负荷开关、柱上隔离开关） -->
<template>
  <el-dialog
    @close="close"
    :visible="show"
    append-to-body
    :close-on-click-modal="false"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(23,69,125,0.9)"
    width="500px"
  >
    <el-form :model="form" :rules="rules" label-width="150px" ref="form">
      <el-form-item label="设备名称" prop="sbmc">
        <el-input v-model="form.sbmc" maxlength="400" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="电压等级" prop="dydj">
        <el-select v-model="form.dydj" disabled>
          <el-option
            v-for="item in commonOpts.dydjOpt"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="投运日期" prop="tyrq">
        <el-date-picker
          v-model="form.tyrq"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否实现配电自动化" prop="sfsxpdzdh">
        <el-select v-model="form.sfsxpdzdh" @change="changeSfsxpdzdh">
          <el-option
            v-for="item in commonOpts.sfOpt"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配电自动化实现方式" prop="pdzdhsxfs">
        <el-select v-model="form.pdzdhsxfs" :disabled="!form.sfsxpdzdh">
          <el-option
            v-for="item in commonOpts.pdzdhsxfsOpt"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="常态运行状态" prop="kgzt">
        <el-select v-model="form.kgzt">
          <el-option
            v-for="item in commonOpts.kgctyxztOpt"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button type="primary" @click="save">设备保存</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import saveMethods from './saveMethods';
export default {
  name: 'Zskg',
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    getMap: Function,
    type: String,
    params: Object
  },
  mixins: [saveMethods],
  data() {
    return {
      loading: false,
      commonOpts: {
        dydjOpt: [{ value: 22, text: '10kV' }],
        sfOpt: [
          { value: 0, text: '否' },
          { value: 1, text: '是' }
        ],
        pdzdhsxfsOpt: [
          { value: 1, text: '三遥终端；' },
          { value: 2, text: '标准型二遥终端；' },
          { value: 3, text: '动作型二遥终端；' },
          { value: 4, text: '基本型二遥终端；' }
        ],
        kgctyxztOpt: [
          { value: 0, text: '常开' },
          { value: 1, text: '常闭' }
        ]
      },
      form: {
        sbmc: '',
        dydj: 22,
        sfsxpdzdh: 1,
        pdzdhsxfs: 1,
        kgzt: 1,
        tyrq: '2022-01-01'
      },
      rules: {
        sbmc: [{ required: true, message: '请输入设备名称', trigger: 'change' }],
        dydj: [{ required: true, message: '请选择电压等级', trigger: 'change' }],
        sfsxpdzdh: [{ required: true, message: '请选择是否实现配电自动化', trigger: 'change' }],
        kgzt: [{ required: true, message: '请选择常态运行状态', trigger: 'change' }],
        tyrq: [{ required: true, message: '请选择投运日期', trigger: 'change' }]
      }
    };
  },
  watch: {
    show(newVal) {
      if (newVal) {
        this.initParams();
      }
    }
  },
  methods: {
    initParams() {
      this.$nextTick(() => {
        this.form.sbmc = this.params.data.otherfields.sbmc;
        this.form.dydj = this.params.data.otherfields.dydj;
      });
    },
    changeSfsxpdzdh(val) {
      if (!val) {
        this.form.pdzdhsxfs = '';
      }
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.type === 'add') {
            this.saveNewDevice().then(() => {
              this.loading = false;
              this.$message({
                type: 'success',
                message: '保存' + this.form.sbmc + '成功'
              });
              this.close();
            });
          } else if (this.type === 'edit') {
            Promise.all([this.saveDeviceProperties(), this.saveDeviceAndLinesShape()]).then(() => {
              this.loading = false;
              this.$message({
                type: 'success',
                message: '保存' + this.form.sbmc + '成功'
              });
              this.close();
            });
          }
        }
      });
    },
    saveNewDevice() {
      return new Promise((resolve) => {
        let params = this.params.data;
        params.otherfields = Object.assign({}, params.otherfields, this.form);
        this.getMap()
          .api.PwghGhsjTxfw.sbjh.post_sbjh_addEquips({
            params: { verRule: this.params.defaultOptions.verRule },
            data: params
          })
          .then((res) => {
            if (res.resp_code === 200) {
              resolve();
            }
          });
      });
    },
    close() {
      this.$refs.form.resetFields();
      this.$emit('update:show', false);
      this.$emit('refresh');
    }
  }
};
</script>

<style lang="less" scoped></style>
